package com.me.web.system.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Date;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.me.common.core.domain.entity.SysRole;
import com.me.common.core.domain.entity.SysUser;
import com.me.common.utils.SecurityUtils;
import com.me.common.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.me.web.system.domain.SysGroupRole;
import com.me.web.system.service.ISysRoleService;
import com.me.web.system.service.SysGroupRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.me.web.system.mapper.SysGroupMapper;
import com.me.web.system.domain.SysGroup;
import com.me.web.system.service.SysGroupService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 用户组管理Service业务层处理
 *
 * @author me
 * @date 2024-06-22
 */
@Service
public class SysGroupServiceImpl extends ServiceImpl<SysGroupMapper, SysGroup> implements SysGroupService {

    @Resource
    ISysRoleService sysRoleService;
    @Resource
    SysGroupRoleService sysGroupRoleService;

    /**
     * 查询用户组管理
     *
     * @param id 用户组管理ID
     * @return 用户组管理
     */
    @Override
    public SysGroup get(Long id) {
        SysGroup sysGroup = this.getById(id);
        List<SysGroupRole> sysGroupRoles = sysGroupRoleService.list(new LambdaQueryWrapper<SysGroupRole>()
                .eq(SysGroupRole::getGroupId, id));
        List<Long> roleIds = sysGroupRoles.stream().map(SysGroupRole::getRoleId).collect(Collectors.toList());
        sysGroup.setGroupRoleIdList(roleIds);
        return sysGroup;
    }

    /**
     * 查询用户组管理列表
     *
     * @param sysGroup 用户组管理
     * @return 用户组管理
     */
    @Override
    public List<SysGroup> queryList(SysGroup sysGroup) {
        // 注意：mybatis-plus lambda 模式不支持 eclipse 的编译器
        // LambdaQueryWrapper<SysGroup> queryWrapper = Wrappers.lambdaQuery();
        //QueryWrapper<SysGroup> queryWrapper = Wrappers.query();
        QueryWrapper<SysGroup> wrapper = Wrappers.query();
        if (sysGroup != null) {
            wrapper.like(StringUtils.isNotEmpty(sysGroup.getGroupCode()), "g.group_code", sysGroup.getGroupCode());
            wrapper.like(StringUtils.isNotEmpty(sysGroup.getGroupName()), "g.group_name", sysGroup.getGroupName());
            wrapper.eq(StringUtils.isNotEmpty(sysGroup.getStatus()), "g.`status`", sysGroup.getStatus());
        }
        wrapper.groupBy("g.id").orderByAsc("g.group_sort");
        List<SysGroup> list = this.baseMapper.getGroupRoleList(wrapper);
        return list;
    }

    /**
     * 新增用户组管理
     *
     * @param sysGroup 用户组管理
     * @return 结果
     */
    @Override
    @Transactional
    public Boolean insert(SysGroup sysGroup) {
        sysGroup.setCreateTime(new Date());
        sysGroup.setCreateBy(SecurityUtils.getNickName());
        sysGroup.setUpdateTime(sysGroup.getCreateTime());
        sysGroup.setUpdateBy(SecurityUtils.getNickName());
        boolean save = this.save(sysGroup);
        if (save) {
            setGroupRole(sysGroup);
        }
        return save;
    }

    /**
     * 修改用户组管理
     *
     * @param sysGroup 用户组管理
     * @return 结果
     */
    @Override
    @Transactional
    public Boolean update(SysGroup sysGroup) {
        sysGroup.setUpdateTime(new Date());
        sysGroup.setUpdateBy(SecurityUtils.getNickName());
        boolean update = this.updateById(sysGroup);
        if (update) {
            setGroupRole(sysGroup);
        }
        return update;
    }

    public void setGroupRole(SysGroup sysGroup) {
        sysGroupRoleService.remove(new LambdaQueryWrapper<SysGroupRole>()
                .eq(SysGroupRole::getGroupId, sysGroup.getId()));
        if (sysGroup.getGroupRoleIdList().size() > 0) {
            List<SysGroupRole> groupRoles = new ArrayList<>();
            for (Long roleId : sysGroup.getGroupRoleIdList()) {
                SysGroupRole sysGroupRole = new SysGroupRole();
                sysGroupRole.setGroupId(sysGroup.getId());
                sysGroupRole.setRoleId(roleId);
                groupRoles.add(sysGroupRole);
            }
            sysGroupRoleService.saveBatch(groupRoles);
        }
    }

    /**
     * 批量删除用户组管理
     *
     * @param ids 需要删除的用户组管理ID
     * @return 结果
     */
    @Override
    @Transactional
    public Boolean delete(Long[] ids) {
        List<Long> delIds = Arrays.asList(ids);
        boolean del = this.removeByIds(delIds);
        if (del) {
            sysGroupRoleService.remove(new LambdaQueryWrapper<SysGroupRole>()
                    .in(SysGroupRole::getGroupId, delIds));
        }
        return del;
    }

    /**
     * 获取所有的角色
     *
     * @return
     */
    @Override
    public List<SysRole> getAllRoles() {
        return sysRoleService.list();
    }


}
